/*****************************************************************************
 * stack.c: Silly  stack used for temporarily storing pointers
 *****************************************************************************
 * Copyright (C) 2012
 *
 * Authors: Dan Larkin <danielhlarkin@gmail.com>
 *          Yonatan Naamad <cached@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *****************************************************************************/

#include <stdlib.h>
#include "stack.h"

void cpm_stack_push( cpm_stack *stack, void *element )
{
    cpm_stack_node *node = calloc( 1, sizeof( cpm_stack_node ) );
    node->element = element;
    node->next = stack->head;
    stack->head = node;
}

void* cpm_stack_pop( cpm_stack *stack )
{
    cpm_stack_node *node = stack->head;
    void* element = NULL;
    
    if( node != NULL )
    {
        element = node->element;
        stack->head = node->next;
        free( node );
    }

    return element;
}

int cpm_stack_empty( cpm_stack *stack )
{
    return( stack->head == NULL );
}
